package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.Menu;
import com.dk.pojo.TPerson;
import com.dk.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menu")
public class MenuController {
    @Reference(timeout = 3000)
    private MenuService menuService;

    @RequestMapping("toShow")
    public String toShow(){
        return "index";
    }
    @RequestMapping("getInfo")
    @ResponseBody
    public List<Map<String ,Object>> getInfo(HttpServletRequest request){
      /*  TPerson tp = (TPerson) request.getSession().getAttribute("person");
        System.out.println(tp.getPname()+tp.getPid());*/
        TPerson tp = (TPerson)SecurityUtils.getSubject().getPrincipal();
        List<Map<String ,Object>> list = menuService.getInfo(tp.getPid());
        return  list;
    }
    @RequestMapping("toTree")
    public String toTree(){
        return "menu/tree";
    }
    @RequestMapping("getInfoTree")
    @ResponseBody
    public List<Map<String ,Object>> getInfoZtree(){
        List<Map<String ,Object>> list= menuService.getInfoZtree();
        return list;
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "menu/add";
    }
    @RequestMapping("increase")
    @ResponseBody
    public boolean increase(Menu menu){
        return  menuService.insert(menu) > 0;
    }
    @RequestMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable("id")Integer id, Model model){
      Menu menu =  menuService.selectById(id);
      model.addAttribute("menu",menu);
      return "menu/update";
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public boolean updateInfo(Menu menu){
        return menuService.updateInfo(menu) > 0;
    }
    @RequestMapping("getInfoZtreeChecked/{roleId}")
    @ResponseBody
    public List<Map<String ,Object>> getInfoZtreeChecked(@PathVariable("roleId")Integer roleId){
        List<Map<String ,Object>> list = menuService.getInfoZtreeChecked(roleId);
        return list;
    }
    @RequestMapping("deleteById")
    @ResponseBody
    public boolean deleteById(Integer id){
       return menuService.deleteById(id) > 0;
    }


}
